package com.jxnu.project_z_android.ui.main;

import android.app.Activity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MainViewModel extends ViewModel {
    private Activity activity;
    private MutableLiveData<String> StringData;


    public MainViewModel() {
        StringData = new MutableLiveData<>();
        StringData.setValue("peter chans");
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public MutableLiveData<String> getStringData() {
        return StringData;
    }

    public void updateText() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Document document = null;
                try {
                    document = Jsoup.connect("https://www.baidu.com/").get();
                    final String title = document.title();

                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            getStringData().setValue(title);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
